home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 9921 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  5.5 KB

  1. Path: goanna.cs.rmit.EDU.AU!not-for-mail
  2. From: ok@goanna.cs.rmit.EDU.AU (Richard A. O'Keefe)
  3. Newsgroups: comp.lang.ada,comp.lang.c++,comp.lang.c
  4. Subject: Re: Hungarian notation - whoops!
  5. Date: 5 Mar 1996 15:22:20 +1100
  6. Organization: Comp Sci, RMIT, Melbourne, Australia
  7. Message-ID: <4hgfhs$74l@goanna.cs.rmit.EDU.AU>
  8. References: <30C40F77.53B5@swsbbs.com> <4fc157$jsf@goanna.cs.rmit.EDU.AU> <dewar.823793746@schonberg> <4fms62$c0p@goanna.cs.rmit.EDU.AU> <4ft1ruINN6dr@keats.ugrad.cs.ubc.ca> <4g9255$74s@goanna.cs.rmit.EDU.AU> <824909942snz@genesis.demon.co.uk> <4h6gbp$guh@goanna.cs.rmit.EDU.AU> <dewar.825776096@schonberg>
  9. NNTP-Posting-Host: goanna.cs.rmit.edu.au
  10. X-Newsreader: NN version 6.5.0 #0 (NOV)
  11.  
  12. dewar@cs.nyu.edu (Robert Dewar) writes:
  13. >Richard O'Keefe said
  14.  
  15. >"I have implemented multiple-precision arithmetic, and I must say I found
  16. >2s-complement a confounded nuisance.
  17.  
  18. >This makes me think that you don't understand the point. What you say above
  19. >is that for multiple-precision you need unsigned arithmetic, and that signs
  20. >get in the way.
  21.  
  22. No, I didn't say that at all.  What I said was that the way _I_ implemented
  23. multiprecision arithmetic >>made use of<< unsigned arithmetic and that signs
  24. would have got in my way.  Obviously all you absolutely >>need<< for
  25. implementing any kind of arithmetic is operations on single bits.  (Think of
  26. the DAP, where floating point arithmetic was implemented by single-bit CPUs.)
  27.  
  28. >But the whole point is that TWOS COMPLEMENT ARITHMETIC IS
  29. >IDENTICAL TO UNSIGNED (for addition and subtraction).
  30.  
  31. Wrong.  That is not the whole point.  In fact, it isn't even true on some
  32. machines.  There are machines where signed twos-complement add and subtract
  33. are done by one set of instructions, which raise an exception on overflow,
  34. and unsigned arithmetic is done by another set of instructions, which don't.
  35.  
  36. In any case, addition and subtraction are hardly the whole of mult-precision
  37. arithmetic.  Multiplication and division and remainder are also required,
  38. and twos complement multiplication division and remainder are most certainly
  39. NOT identical to unsigned.
  40.  
  41. Mind you, somebody in the GCC crowd appears to think they are.  On a SPARC,
  42. signed integer multiplication is done by the run-time library function .mul
  43. and unsigned integer multiplication is done by .umul.  Here's a C function:
  44.  
  45. int om(int left, int right) { return left * right ; }
  46.  
  47. Compile it using LCC and you get
  48.  
  49.     om:
  50.     save    %sp,-96,%sp
  51.     mov    %i0,%o0
  52.     mov    %i1,%o1
  53.     call    .mul,2
  54.     nop
  55.     mov    %o0,%i0
  56.     ret
  57.     restore
  58.  
  59. Compile it using SPARCompiler C and you get
  60.  
  61.     om:
  62.     save    %sp,-96,%sp
  63.     or    %g0,%i1,%o1        ; same as mov %i1,%o1
  64.     call     .mul,2
  65.     or    %g0,%i0,%o0        ; same as mov %i1,%o1
  66.     ret
  67.     restore    %g0,%o0,%o0
  68.  
  69. But compile it with GCC and you get
  70.  
  71.     om:
  72.     save    %sp,-112,%sp
  73.     mov    %i0,%o0
  74.     call    .umul,0            ; note .umul,0 instead of .mul,2
  75.     mov    %i1,%o1
  76.     mov    %o0,%i0
  77.     ret
  78.     restore
  79.  
  80. "The whole point" is that twos-complement arithmetic and unsigned arithmetic
  81. are DIFFERENT (except for addition and subtraction) and here GCC is calling
  82. the _unsigned multiplication_ routine to multiply _signed_ numbers.  In
  83. the SPARC V8 ABI manual, .mul is listed in appendix E.1 and .umul is
  84. listed in appendix E.2.  In fact the functions listed there _do_ report
  85. overflow by setting the Z bit.  For multiplication, you can get away
  86. with using the unsigned multiply as gcc does, but then you lose the
  87. ability to test the Z bit.  (Which matters for the -gnato option.)
  88.  
  89. >If you had trouble from 2s-complement arithmetic in this context, it must
  90. >have been because you were confused ..
  91.  
  92. No, it was because I was doing comparison and multiplication and
  93. division as well as addition and subtraction.
  94.  
  95. In particular, if you are doing 16x16->32 bit multiplication, which is
  96. useful for multiprecision arithmetic, signed multiply and unsigned 
  97. multiply give very different answers.
  98.  
  99. >As for the claim that you don't have to check for overflow in negation,
  100. >that's preetty much irrelevant in practice, since in a properly 
  101. >written program with reasonable types, there is no reason that the range
  102. >of your data should correspond to machine ranges anyway.
  103.  
  104. This is perfectly true *IF* you are using a language that lets you say
  105. what range you want *AND* you are in charge of the interface.  But this
  106. discussion is not confined to comp.lang.ada and I for one hadn't been
  107. considering only small single-author programs.  Robert Dewar also ignores
  108. my specific claim that I had encountered Pascal compilers which failed
  109. to check for overflow after negation or absolute value.
  110.  
  111. Perhaps we should wind this down.  Clearly this is an area where reasonable
  112. people may differ.  I am quite certain that Robert Dewar and I are agreed
  113. that a well-written program in a well-chosen programming language specifies
  114. the bounds that are appropriate to that program for that problem rather than
  115. blindly accepting some machine size, and that a good compiler will, on
  116. request, generate sufficient run-time checks (I have seen figures that 99+%
  117. of bounds checks can be optimised away) that overflow beyond the declared
  118. range will certainly be trapped.  In such an environment, the fact that the
  119. underlying arithmetic is unpleasant (my view) or wonderful (Dewar's view)
  120. doesn't matter, because the programmer only has to deal with the arithmetic
  121. model of the language, not the machine.
  122.  
  123. Not all programs can be written in such languages or with the aid of such
  124. compilers, and all too often the machine shows through.
  125.  
  126. -- 
  127. The election is over, and Australia lost; the idjits elected _politicians_!
  128. Richard A. O'Keefe; http://www.cs.rmit.edu.au/~ok; RMIT Comp.Sci.
  129.